package menu;

import static game.Configurations.SCREEN_HEIGHT;
import static game.Configurations.SCREEN_WIDTH;
import game.Controls;
import static game.Controls.Key.GO_DOWN;
import static game.Controls.Key.GO_UP;
import static game.Controls.Key.SEL_DOWN;
import static game.Controls.Key.SEL_LEFT;
import static game.Controls.Key.SEL_RIGHT;
import static game.Controls.Key.SEL_UP;
import static game.Controls.Key.ZOOM_IN;
import static game.Controls.Key.ZOOM_OUT;
import game.FontLoader;
import game.Data;
import images.BlockLoader;
import images.MenuLoader;
import images.ShipLoader;
import java.awt.Color;
import saves.Area;
import saves.SaveManager.Profile;
import saves.objects.Nave;
import saves.objects.Corpo;
import saves.objects.SeletorBloco;

/**
 *
 * @author Luan
 */
public class AreaMap implements Menu {

    private Menu parent;
    private Area area;
    private int lastRef;
    public static int MAP_BLOCK_SIZE = 8;

    public AreaMap(Menu parent, Area area) {
        this.parent = parent;
        this.area = area;
    }

    public void prepareArea() {
        area.addObjeto(new SeletorBloco(0, 0, Color.BLUE));
        this.lastRef = area.setReferencia();
    }

    @Override
    public Menu getParent() {
        return parent;
    }

    @Override
    public void setParent(Menu parent) {
        this.parent = parent;
    }

    @Override
    public void draw() {
        MenuLoader.bindTexture();
        MenuLoader.drawBack();

        FontLoader.MENU.drawString(10, 20 - FontLoader.MENU.getLineHeight() / 2, "Area Map"); //todo start using textmanager
        area.desenharMapa(10, 50, SCREEN_WIDTH - 160, SCREEN_HEIGHT - 60, MAP_BLOCK_SIZE, false); //desenha o mapa
        //desenhar bloco selecionado
        if (area.getBlocoOver() != null) {
            FontLoader.TEXT_SMALL.drawString(SCREEN_WIDTH - 130, 50, "Selected block:");
            Data.BlocoInfo bi = Data.getBlocoInfo(area.getBlocoOver().getCodigo());
            FontLoader.TEXT_BIG.drawString(SCREEN_WIDTH - 130 + 35, 70, bi.getNome());
            FontLoader.TEXT_SMALL.drawString(SCREEN_WIDTH - 130, 100, "Resistência: " + bi.getResistenciaAr());
            FontLoader.TEXT_SMALL.drawString(SCREEN_WIDTH - 130, 115, "Opacidade: " + bi.getCoeficienteIluminacao());
            FontLoader.TEXT_SMALL.drawString(SCREEN_WIDTH - 130, 130, "Iluminação: " + bi.getIluminacaoPropria());
            BlockLoader.bindBlocks();
            BlockLoader.desenharBloco(area.getBlocoOver(), SCREEN_WIDTH - 130, 70, 32);

            //desenhar objeto selecionado
            Corpo o = area.getObjectOver();
            if (o != null)
                //todo fazer outros tipos de objetos
                if (o instanceof Nave) { //se for nave
                    FontLoader.TEXT_SMALL.drawString(SCREEN_WIDTH - 130, 155, "Selected object:");
                    FontLoader.TEXT_BIG.drawString(SCREEN_WIDTH - 130 + 35, 175, ((Nave) o).getEntidade().getIdentificacao() + " - " + ((Nave) o).getEntidade().getNome());
                    ShipLoader.bindTexture();
                    ShipLoader.draw(((Nave) o).getEntidade(), SCREEN_WIDTH - 130, 175, 32, 0, 0);
                }
        }
    }

    @Override
    public void dispose() {
        area.removerSeletores(lastRef);
    }

    public void moveH(boolean right) {
        SeletorBloco s = ((SeletorBloco) area.getReferencia());
        if (area.contem(s.getPixelX() + (right ? 1 : -1), s.getPixelY()))
            s.mover(right ? 1 : -1, 0);
    }

    public void moveV(boolean up) {
        SeletorBloco s = ((SeletorBloco) area.getReferencia());
        if (area.contem(s.getPixelX(), s.getPixelY() + (up ? 1 : -1)))
            s.mover(0, up ? 1 : -1);
    }

    //todo implemente these
    public void moveCH(boolean right) {
    }

    public void moveCV(boolean down) {
    }
    
    @Override
    public void parse(int k, char c) {
        Controls.Key key = Controls.get(k, true);
        if (key == SEL_UP)
            moveV(false);
        else if (key == SEL_DOWN)
            moveV(true);

        if (key == SEL_LEFT)
            moveH(false);
        else if (key == SEL_RIGHT)
            moveH(true);

        if (key == GO_UP)
            moveCV(false);
        else if (key == GO_DOWN)
            moveCV(true);

        if (key == ZOOM_IN)
            moveCH(true);
        else if (key == ZOOM_OUT)
            moveCH(false);
    }

    @Override
    public Menu go() {
        return null;
    } //maybe open chat if over other ship or something

    @Override
    public int getActionCode() {
        return 0;
    }

    @Override
    public Menu back() {
        return parent;
    }

    @Override
    public Menu setProfile(Profile save) {
        return this;
    }

    @Override
    public Menu zerar() {
        return this;
    }
}